<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="editPasswordRef" label-width="120px" style="margin-left: 120px; margin-top: 30px" :model="userInfo" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width: 300px" />
              </el-form-item>
              <el-form-item label="新密码:" prop="password">
                <el-input v-model="userInfo.password" style="width: 300px" type="password" />
              </el-form-item>
              <el-form-item label="确认新密码:" prop="repassword">
                <el-input v-model="userInfo.repassword" style="width: 300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUserSimpleInfo">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <user-info />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
// 导入员工个人详情组件
import UserInfo from './user-info.vue'
// 导入员工岗位信息组件
import JobInfo from './job-info.vue'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    const checkRePassword = (rule, value, callback) => {
      if (this.userInfo.password !== value) return callback(new Error('两次输入的密码不一致'))
      callback()
    }
    return {
      // 待修改的用户id
      userId: this.$route.params.id,
      //  专门存放基本信息
      userInfo: {
        username: '',
        password: '',
        repassword: ''
      },
      // 验证规则
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '确认密码长度6-9位', trigger: 'blur' },
          { validator: checkRePassword, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    // 获取用户的基本信息
    async getUserDetailById() {
      const res = await getUserDetailById(this.userId)
      this.userInfo.username = res.username
    },

    // 更新用户的姓名和密码
    updateUserSimpleInfo() {
      this.$refs.editPasswordRef.validate(async valid => {
        if (!valid) return false
        try {
          await saveUserDetailById({ ...this.userInfo, id: this.userId })
          this.$message.success('更新密码成功')
          this.$router.back()
        } catch {
          // todo
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-form-item__label {
    font-weight: 400;
  }
}
</style>
